#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/kthread.h>
#include <linux/delay.h>

static wait_queue_head_t my_queue;
static int condition = 0;

static int waiter_thread(void *data)
{
	printk(KERN_INFO "Waiter thread started\n");

	wait_event(my_queue, condition != 0);

	printk(KERN_INFO "Waiter thread woke up, condition=%d\n", condition);
	return 0;
}

static int __init wait_queue_example_init(void)
{
	struct task_struct *task;

	init_waitqueue_head(&my_queue);

	task = kthread_run(waiter_thread, NULL, "waiter_thread");
	if (IS_ERR(task)) {
		printk(KERN_ERR "Failed to create thread\n");
		return PTR_ERR(task);
	}

	msleep(5000);		// 模拟条件变化的延迟
	condition = 1;
	wake_up(&my_queue);

	return 0;
}

static void __exit wait_queue_example_exit(void)
{
	printk(KERN_INFO "Module exiting\n");
}

module_init(wait_queue_example_init);
module_exit(wait_queue_example_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux Enthusiast");
MODULE_DESCRIPTION("Simple wait_queue example");
